/*     */ package com.zimbra.cs.client.soap;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.DomUtil;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.client.LmcMessage;
/*     */ import com.zimbra.cs.client.LmcSession;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.net.FileNameMap;
/*     */ import java.net.URLConnection;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.apache.commons.httpclient.Cookie;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.multipart.FilePart;
/*     */ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.multipart.Part;
/*     */ import org.apache.commons.httpclient.params.HttpClientParams;
/*     */ import org.apache.commons.httpclient.params.HttpMethodParams;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LmcSendMsgRequest
/*     */   extends LmcSoapRequest
/*     */ {
/*     */   private LmcMessage mMsg;
/*     */   private String mInReplyTo;
/*     */   private String mFwdMsgID;
/*     */   private String[] mFwdPartNumbers;
/*     */   
/*  53 */   public void setMsg(LmcMessage m) { this.mMsg = m; }
/*     */   
/*  55 */   public LmcMessage getMsg() { return this.mMsg; }
/*     */   
/*     */   public void setReplyInfo(String inReplyTo) {
/*  58 */     this.mInReplyTo = inReplyTo;
/*     */   }
/*     */   
/*     */   public void setFwdInfo(String inReplyTo, String fwdMsgID, String[] fwdPartNumbers) {
/*  62 */     this.mInReplyTo = inReplyTo;
/*  63 */     this.mFwdMsgID = fwdMsgID;
/*  64 */     this.mFwdPartNumbers = fwdPartNumbers;
/*     */   }
/*     */   
/*     */   protected Element getRequestXML() {
/*  68 */     Element request = DocumentHelper.createElement(MailConstants.SEND_MSG_REQUEST);
/*  69 */     addMsg(request, this.mMsg, this.mInReplyTo, this.mFwdMsgID, this.mFwdPartNumbers);
/*  70 */     return request;
/*     */   }
/*     */   
/*     */ 
/*     */   protected LmcSoapResponse parseResponseXML(Element responseXML)
/*     */     throws ServiceException
/*     */   {
/*  77 */     Element m = DomUtil.get(responseXML, "m");
/*  78 */     LmcSendMsgResponse response = new LmcSendMsgResponse();
/*  79 */     response.setID(m.attributeValue("id"));
/*  80 */     return response;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String postAttachment(String uploadURL, LmcSession session, File f, String domain, int msTimeout)
/*     */     throws LmcSoapClientException, IOException
/*     */   {
/*  93 */     String aid = null;
/*     */     
/*     */ 
/*  96 */     if (session == null) {
/*  97 */       System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcSendMsgRequest.postAttachment session=null");
/*     */     }
/*  99 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 100 */     PostMethod post = new PostMethod(uploadURL);
/* 101 */     ZAuthToken zat = session.getAuthToken();
/* 102 */     Map<String, String> cookieMap = zat.cookieMap(false);
/* 103 */     if (cookieMap != null) {
/* 104 */       HttpState initialState = new HttpState();
/* 105 */       for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
/* 106 */         Cookie cookie = new Cookie(domain, (String)ck.getKey(), (String)ck.getValue(), "/", -1, false);
/* 107 */         initialState.addCookie(cookie);
/*     */       }
/* 109 */       client.setState(initialState);
/* 110 */       client.getParams().setCookiePolicy("compatibility");
/*     */     }
/* 112 */     post.getParams().setSoTimeout(msTimeout);
/* 113 */     int statusCode = -1;
/*     */     try {
/* 115 */       String contentType = URLConnection.getFileNameMap().getContentTypeFor(f.getName());
/* 116 */       Part[] parts = { new FilePart(f.getName(), f, contentType, "UTF-8") };
/* 117 */       post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
/* 118 */       statusCode = HttpClientUtil.executeMethod(client, post);
/*     */       
/*     */ 
/* 121 */       if (statusCode == 200)
/*     */       {
/* 123 */         String response = post.getResponseBodyAsString();
/*     */         
/* 125 */         int lastQuote = response.lastIndexOf("'");
/* 126 */         int firstQuote = response.indexOf("','") + 3;
/* 127 */         if ((lastQuote == -1) || (firstQuote == -1))
/* 128 */           throw new LmcSoapClientException("Attachment post failed, unexpected response: " + response);
/* 129 */         aid = response.substring(firstQuote, lastQuote);
/*     */       } else {
/* 131 */         throw new LmcSoapClientException("Attachment post failed, status=" + statusCode);
/*     */       }
/*     */     } catch (IOException e) {
/* 134 */       System.err.println("Attachment post failed");
/*     */       
/* 136 */       throw e;
/*     */     } finally {
/* 138 */       post.releaseConnection();
/*     */     }
/*     */     
/* 141 */     return aid;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/client/soap/LmcSendMsgRequest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */